//logs.js
const util = require('../../utils/util.js')
const app = getApp();

Page({
    data: {
        circleSize: 0,
        progressValue: -1,
        progressBar: 0,
        userList: [],
        act: "",
        clickNumber: 0,
        show: false,
        iconSrc: "",
        userName: "",
        columns: [],
        showAct: false,
        historyAct: ""
    },
    onShareAppMessage() {
        qq.showShareMenu({
            showShareItems: ['qq', 'qzone', 'wechatFriends', 'wechatMoment']
        });
    },
    async reloadTable() {
        const db = qq.cloud.database();
        let Size = 150 / 320;
        let speed = 0;
        let list = [];
        let participant = 0;
        const _ = db.command;
        let columns = [];
        let act = "";
        this.setData({ progressBar: 0 });
        await db.collection("act")
            .where({
                'alliance': app.data.alliance
            })
            .get()
            .then(res => {
                columns = res.data[0].act;
                this.setData({ historyAct: res.data[0].act[0] });
                act = columns[columns.length - 1];
                if (columns.length != this.data.columns.length) {
                    this.setData({ act: act });
                    this.setData({ columns: columns });
                }
            });
        await db.collection("sign-in")
            .where({
                'alliance': app.data.alliance
            })
            .get()
            .then(function(res) {
                list = res.data;
            });
        await db.collection("controlSign-in")
            .where({
                'alliance': app.data.alliance
            })
            .get()
            .then(function(res) {
                res.data.map((value) => {
                    list.push(value);
                });
            });
        this.setData({ userList: list });
        for (let i = 0; i < list.length; i++) {
            if (list[i][this.data.act] == "√") {
                participant++;
            }
        }
        this.setData({ progressValue: (participant / list.length) * 100 });
        qq.getSystemInfo({
            success(res) {
                Size *= res.windowWidth;

            }
        })
        this.setData({ circleSize: Size });
        if (list.length) {
            let load = () => {
                setTimeout(() => {
                    speed++;
                    if (Math.floor(this.data.progressValue) != this.data.progressBar) {
                        this.setData({ progressBar: speed });
                        load();
                    } else if (this.data.progressValue <= this.data.progressBar) {
                        clearInterval(load);
                    }
                }, 10);
            }
            load();
        }
    },
    async onHide() {
        this.reloadTable();
    },
    async onShow() {
        this.reloadTable();
    },
    async showIcon(event) {
        this.setData({ show: true });
        this.setData({ userName: event.target.dataset.username });
        await qq.cloud.downloadFile({
            fileID: `cloud://ww-8g97rzjc90e04d38.7777-ww-8g97rzjc90e04d38-1304817035/${event.target.dataset.username}/${this.data.act}.jpg`,
            success: res => {
                console.log(res.tempFilePath);
                this.setData({ iconSrc: res.tempFilePath });
            },
            fail: err => {
                this.setData({ iconSrc: "null" });
                console.log(err);
            }
        });
    },
    onClickHide() {
        this.setData({ show: false });
        this.setData({ iconSrc: "" });
    },
    switchAct() {
        this.setData({ showAct: true });
    },
    onClose() {
        this.setData({ showAct: false });
    },
    onChange(event) {
        const { picker, value, index } = event.detail;
        this.setData({ historyAct: value });
    },
    onClickLeft() {
        this.setData({ showAct: false });
    },
    async onClickRight() {
        const db = qq.cloud.database();
        this.setData({ showAct: false });
        this.setData({ act: this.data.historyAct });
        app.data.act = this.data.historyAct;
        let participant = 0;
        let Size = 150 / 320;
        let speed = 0;
        let list = [];
        this.setData({ progressBar: 0 });
        await db.collection("sign-in")
            .where({
                'alliance': app.data.alliance
            })
            .get()
            .then(function(res) {
                list = res.data;
            });
        await db.collection("controlSign-in")
            .where({
                'alliance': app.data.alliance
            })
            .get()
            .then(function(res) {
                res.data.map((value) => {
                    list.push(value);
                });
            });
        this.setData({ userList: list });
        for (let i = 0; i < list.length; i++) {
            if (list[i][this.data.act] == "√") {
                participant++;
            }
        }
        this.setData({ progressValue: (participant / list.length) * 100 });
        if (list.length) {
            let load = () => {
                setTimeout(() => {
                    speed++;
                    if (Math.floor(this.data.progressValue) != this.data.progressBar) {
                        this.setData({ progressBar: speed });
                        load();
                    } else if (this.data.progressValue <= this.data.progressBar) {
                        clearInterval(load);
                    }
                }, 10);
            }
            load();
        }
    },

})